package com.yxjx.duoxue;

import org.json.JSONException;
import org.json.JSONObject;

import com.yxjx.duoxue.ad.AdPageActivity;
import com.yxjx.duoxue.datamodel.DMObject;
import com.yxjx.duoxue.my.UserLoginActivity;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.push.PushHelper;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter.LengthFilter;

/**
 * 格式的format如下： 课程：src="duoxueapp://data/course/1212"
 * 学校：src="duoxueapp://data/school/1212" 老师：src="duoxueapp://data/teacher/1212"
 * 活动：src="duoxueapp://data/activity/1212"
 * 
 * 套餐详情：src="duoxueapp://data/product/1212"
 * 订单详情：src="duoxueapp://data/orderinfo/1212"
 * 
 * 代金劵：src="duoxueapp://data/page/mycoupon 我的订单
 * src="duoxueapp://data/page/myorders 专题: http://xxxx/xxx/xxxx; XXX活动啦
 * 
 * 
 * 付费协议名 网页：src="duoxueapp://data/pay" 付费协议名：多学用户付款协议
 * 付费协议url：http://www.learnmore.com.cn/index/userPayAgreement <a
 * hrep="duoxueapp://data/activity/1212">
 * 
 * 网页app地址如下：
 * <a hrep="duoxueapp://link/百度/分享百度页面／www.learnmore.com.cn/index/userPayAgreement">
 */
public class JumpActivity extends Activity {

	public static final String PREFIX_DATA = "duoxueapp://data/";
	public static final String PREFIX_LINK = "duoxueapp://link/";

	private static final int PREFIX_TYPE_DATA = 1; // Native页面
	private static final int PREFIX_TYPE_LINK = 2; // H5页面

	private int prefixType = PREFIX_TYPE_DATA;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();

		String data = intent.getDataString();
		Utils.logd("CMM5, data = " + data);

		// 解析data并打开不同的页面
		String postFix = "";
		if (data.startsWith(PREFIX_DATA)) {
			postFix = data.substring(PREFIX_DATA.length());
			prefixType = PREFIX_TYPE_DATA;
		} else if (data.startsWith(PREFIX_LINK)) {
			postFix = data.substring(PREFIX_LINK.length());
			prefixType = PREFIX_TYPE_LINK;
		} else {
			return;
		}

		if (UIHelper.isEmpty(postFix)) {
			finish();
			return;
		}


		if (prefixType == PREFIX_TYPE_DATA) {

			String[] items = new String[2];
			// 课程,学校,老师,活动 “／”分割后只会有两项
			items = postFix.split("/");
			if (items == null || items.length != 2) {
				finish();
				return;
			}

			JSONObject jo = new JSONObject();
			try {
				jo.put(PushHelper.KEY_TYPE, items[0]);
				jo.put(PushHelper.KEY_TYPE_ID, items[1]);
				PushHelper.pushActivity(this, jo.toString());
				this.finish();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else if (prefixType == PREFIX_TYPE_LINK) {
			String[] items = new String[3];
			// 网页有三项：（title，content和link）
			items = postFix.split("/");
			if (items == null || items.length >= 3) {
				finish();
				return;
			}
			
			JSONObject jo = new JSONObject();
			try {
				jo.put(PushHelper.KEY_TYPE, PushHelper.TYPE_AD);
				jo.put(PushHelper.KEY_TITLE, items[0]);
				jo.put(PushHelper.KEY_CONTENT, items[1]);
				jo.put(PushHelper.KEY_URL, postFix.substring(items[0].length() + items[1].length() + 2));
				PushHelper.pushActivity(this, jo.toString());
				this.finish();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		finish();

	}

}
